package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfLoanApplyBasicInfoService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.enums.PlatformRetCodeEnum;
import com.mdb.open.common.enums.trade.status.TradeFileStatusEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.LoanApplyBasicInfoBo;
import com.mdb.open.trade.domain.po.trade.LoanApplyBasicInfo;
import com.mdb.open.trade.mapper.trade.TfLoanApplyBasicInfoMapper;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  贸易融资申请业务基本资料表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfLoanApplyBasicInfoService extends SuperService<TfLoanApplyBasicInfoMapper, LoanApplyBasicInfo> {

    public ResultEx<LoanApplyBasicInfo> saveBasicInfo(LoanApplyBasicInfoBo bo) {

        LoanApplyBasicInfo info = new LoanApplyBasicInfo();
        info.setApplyOrderNo(bo.getApplyOrderNo());
        info.setCustomerCode(bo.getCustomerCode());
        info.setMcustomerCode(bo.getMcustomerCode());
        info.setCustomerName(bo.getCustomerName());
        info.setMerchantNo(bo.getMerchantNo());
        info.setMerchantName(bo.getMerchantName());
        info.setBusinessNo(bo.getBusinessNo());
        info.setBusinessType(bo.getBusinessType());
        info.setImporterCnName(bo.getImporterCnName());
        info.setImporterEnName(bo.getImporterEnName());
        info.setImporterAddress(bo.getImporterAddress());
        info.setCustomsAmount(bo.getCustomsAmount());
        info.setCustomsCnt(bo.getCustomsCnt());
        info.setCustomsTime(bo.getCustomsTime());
        info.setContractAmount(bo.getContractAmount());
        info.setContractCcy(bo.getContractCcy());
        info.setItemsAmount(bo.getItemsAmount());
        info.setStatus(TradeFileStatusEnum.TO_BE_ADDED);
        info.setSourceType(bo.getSourceType());
        info.setApplyTime(bo.getApplyTime());

        int count = this.baseMapper.insert(info);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok(info);
    }

    public ResultEx checkBizNoIsExits(String businessNo, String customerCode){

        int count = this.baseMapper.selectCount(new LambdaQueryWrapper<LoanApplyBasicInfo>()
                .eq(LoanApplyBasicInfo::getCustomerCode, customerCode)
                .eq(LoanApplyBasicInfo::getBusinessNo, businessNo));

        if (count > 0) {
            return ResultEx.makeResult(PlatformRetCodeEnum.APPLY_BUSSINESS_EXIST);
        }
        return ResultEx.ok();
    }

}
